package com.noble.meether.fragment.start;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.BaseFragment;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.image.ImageLoader;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.permission.PermissionUtil;
import com.noble.library.permission.callback.PermissionListener;
import com.noble.library.view.anim.AnimationUtil;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.RtcUIShow;
import com.noble.meether.fragment.rob.util.RobClickListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 男性：发起群聊
 */
public class StartFragment extends BaseFragment implements View.OnClickListener, RequestComplete, PObserver {
    private TextView tips_pop;
    private UserDetail userDetail;
    private ImageView iv_adSpace;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.start_fragment);
        initView();
        return getContentView();
    }

    private void initView() {
        initTipsList();
        iv_adSpace = (ImageView) findViewById(R.id.iv_adSpace);
        initAdSpace();
        tips_pop = (TextView) findViewById(R.id.tips_pop);
        iv_adSpace.setOnClickListener(this);
        findViewById(R.id.btn_start).setOnClickListener(new RobClickListener(1500) {
            @Override
            public void onRobClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_start:
                        start();
                        break;
                }
            }
        });
        MsgMgr.getInstance().attach(this);
    }

    private void initAdSpace() {
        String imgUrl = ModuleMgr.getCommonMgr().getSetting().getUrlHome();
        if (!TextUtils.isEmpty(imgUrl))
            ImageLoader.loadFitCenter(getContext(), imgUrl, iv_adSpace);
    }

    @Override
    public void onResume() {
        super.onResume();
        startTipsAnim();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_adSpace:
                MeetUIShow.showInviteAct(getContext());
                break;
        }
    }

    private void start() {
        if (ModuleMgr.getRtcEnginMgr().getEngineConfig().isBusy) {
            PToast.showShort("当前处于聊天中，无法再次发起");
            return;
        }
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        if (!lessDiamond())
            ModuleMgr.getCenterMgr().reqMyPartInfo(new String[]{"DoPay", "DoGive"}, this);
    }

    private boolean lessDiamond() {
        int minChatStart = ModuleMgr.getCommonMgr().getSetting().getMinChatStart();
        int minSpend = minChatStart > 0 ? minChatStart : 30;
        if (userDetail.getDiamand() < minSpend) {
            MeetUIShow.showPayShareDialog(getActivity());
            return true;
        }
        return false;
    }

    private void checkStart() {
        if (lessDiamond()) return;

        PermissionUtil.checkAVPermission(getActivity(), new PermissionListener() {
            @Override
            public void success() {
                RtcUIShow.showRtcGroupInitAct(getActivity());
            }

            @Override
            public void fail() {
                PToast.showShort("未检测到音视频权限");
            }
        });
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (!response.isOk()) {
            PToast.showShort("请求出错，稍后重试");
            return;
        }
        JSONObject jsonObject = response.getResponseObject();
        userDetail.setDoPay(jsonObject.optInt("DoPay"));
        userDetail.setDoGive(jsonObject.optInt("DoGive"));
        userDetail.setDiamand(userDetail.getDoGive() + userDetail.getDoPay());
        checkStart();
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Setting_Suc:
                initAdSpace();
                break;
        }
    }

    // ============ 提示文字 ==================
    private int tipsPosition = 0;     // 已经提示到的
    private List<String> tips = new ArrayList<>();

    private void initTipsList() {
        tips.add("点击Start让女生争抢");
        tips.add("一键搭讪全国女生");
        tips.add("高颜值女生等你探索~");
        tips.add("30钻石就能撩到高颜值小姐姐");
    }

    private void startTipsAnim() {
        tips_pop.setText(tips.get(tipsPosition));
        tipsPosition++;
        if (tipsPosition == tips.size()) {
            tipsPosition = 0;
            return;
        }
        AnimationUtil.showAndHiddenCircle(tips_pop, 1800, new AnimationUtil.AnimationCallback() {
            @Override
            public void onAnimStart() {
            }

            @Override
            public void onAnimRepeat() {
            }

            @Override
            public void onAnimEnd() {
                startTipsAnim();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }
}

